/*
 * mem_tasks.c
 *
 *  Created on: Jan 17, 2014
 *      Author: Rohit
 */

//INCLUDES
#include "includes.h"

/*
 * Function: set_memory_status
 *
 * Inputs:	address
 * Outputs: data at the address
 *
 * This function sets valid flag for a memory location.
 * Possible values - INVALID_MEM_LOCATION, VALID_LOWER_BYTE, VALID_UPPER_BYTE, VALID_WORD
 */

void set_memory_status(unsigned short status,unsigned short address)
{
	MemStatus[address/2] = status;
}

/*
 * Function: get_memory_status
 *
 * Inputs:	address
 * Outputs: data at the address
 *
 * This function sets valid flag for a memory location.
 * Possible values - INVALID_MEM_LOCATION, VALID_LOWER_BYTE, VALID_UPPER_BYTE, VALID_WORD
 */

MemValidStatus get_memory_status(unsigned short address)
{
	return MemStatus[address/2];
}

/*
 * Function: read_from_memory
 *
 * Inputs:	address
 * Outputs: data at the address
 */

unsigned short read_from_memory(unsigned short address, unsigned short access_type)
{
	if(access_type == DATA)
		write_traces(0, address);
	else if(access_type == INSTRUCTION)
		write_traces(2, address);

	return MemSpace[address/2].word;
}

/*
 * Function: write_in_memory
 *
 * Inputs:	address, data at the address
 * Outputs: {No output}
 */

void write_in_memory(unsigned short address, unsigned short data, unsigned short access_type)
{
	MemSpace[address/2].word = data;

	set_memory_status(VALID_WORD,address);

	if(access_type == DATA)
		write_traces(1, address);
}
